//
//  CourseMoreStudyPlanVC.swift
//  Auscultation
//
//  Created by zwb on 2021/3/28.
//

import UIKit
import HandyJSON

class CourseMoreStudyTestVC: BaseVC {
    var stuId : String = ""
    var model = TestModel()

    var questionView = TestQuestionView(frame: CGRect(x: 0, y: UIDevice.lgf_NavBarH(), width: UIDevice.lgf_ScreenW, height: UIDevice.lgf_ScreenH - UIDevice.lgf_NavBarH() - UIDevice.lgf_BottomSafeAreaH()))

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let top_view = UIImageView()
        top_view.image = UIImage(named: "table2")
        top_view.isUserInteractionEnabled = true
        self.view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }

        
        self.navTitle = "考试"
        let style = LGFNavigationBarStyle.lgf()
        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
        self.lgf_NavigationBar.lgf_line?.isHidden = true
//        self.lgf_NavigationBar.style.lgf_RightBtnTitleColor = UIColor.init(lgf_HexString: "0F0F0F")
//        self.lgf_NavigationBar.style.lgf_TitleColor = UIColor.init(lgf_HexString: "0F0F0F")

        self.lgf_NavigationBar.lgf_Reload()

        view.addSubview(questionView)

        questionView.complmentClouse = { [weak self] json in
            self?.saveTest(paramet: json)

        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    
    @objc override func rightBtnSelect(_ sender: UIButton) {
        navigationController?.pushViewController(TestRecordVC(), animated: true)
    }


}

extension CourseMoreStudyTestVC{
    
    func loadData() {
        
        let paramet  = [
            "userId" : EWEUser.lgf.userId ?? "",
            "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU=",
            "stuId":self.stuId
        ]
        
        EWERequest.lgf("paper/study/" + self.stuId, paramet, .get) {  (data) in
            guard let result = (data as? [String : Any]) else { return  }
            guard let model = TestModel.deserialize(from: result) else { return }
            self.model = model
            self.questionView.dataSource = model.questions
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }
    }

    func saveTest(paramet:[String:Any]) {
        
//        EWERequest.lgf("3dStudy/" + self.stuId, paramet, .get) {  (data) in
//            let vc =  StudyTestViewController()
//            vc.reTestClouse = {
//                self.questionView.resetData()
//                self.questionView.seletedIndex = 0
//
//            }
//            self.navigationController?.pushViewController(vc)
//        } _: {  (error) in
//            SVProgressHUD.showError(error)
//        }
        
        guard let userId = EWEUser.lgf.userId else { return  }
        EWERequest.lgf("paper/" + "\(self.model.id)" + "/user/" + userId, paramet, .post,JSONEncoding.default) {  (data) in
            guard let result = (data as? [String : Any]) else { return  }
            guard let model = TestFinishedModel.deserialize(from: result) else { return }
                let vc =  StudyTestViewController()
            vc.model = model
                vc.reTestClouse = {
                    self.questionView.resetData()
                    self.questionView.seletedIndex = 0
                    self.questionView.result["examBeginTime"] = Date().lgf_TimeStampString()

                }
                self.navigationController?.pushViewController(vc)

        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }
}
